package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.cent.sdata.ProjectPermission;
import com.xhwl.data.dao.IProjectPermissionDao;
import com.xhwl.data.service.IProjectPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 项目与权限
 * @author: lianghz
 * @create: 2020-11-25
 **/
@Service
public class ProjectPermissionServiceImpl extends ServiceImpl<IProjectPermissionDao, ProjectPermission> implements IProjectPermissionService {

    @Autowired
    IProjectPermissionDao projectPermissionDao;

    @Override
    public void save(Integer projectId, List<Integer> permissionIdList) {
        QueryWrapper<ProjectPermission> permissionProjectQueryWrapper = new QueryWrapper<>();
        permissionProjectQueryWrapper.eq("project_id", projectId);
        this.remove(permissionProjectQueryWrapper);
        List<ProjectPermission> projectPermissions = permissionIdList.stream().map(permissionId -> {
            ProjectPermission projectPermission = new ProjectPermission();
            projectPermission.setPermissionId(permissionId);
            projectPermission.setProjectId(projectId);
            return projectPermission;
        }).collect(Collectors.toList());
        this.saveBatch(projectPermissions);
    }

    @Override
    public List<Integer> list(List<Integer> projectIdList) {
        QueryWrapper<ProjectPermission> permissionProjectQueryWrapper = new QueryWrapper<>();
        permissionProjectQueryWrapper
                .select("permission_id")
                .in("project_id", projectIdList);
        List<ProjectPermission> projectPermissions = this.list(permissionProjectQueryWrapper);
        List<Integer> permissionIdList = projectPermissions.stream().map(ProjectPermission::getPermissionId).collect(Collectors.toList());
        return permissionIdList;
    }

    @Override
    public void deleteProjectPermission(List<Integer> projectIdList, List<Integer> permissionIdList) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        if (!CollectionUtils.isEmpty(projectIdList)) {
            updateWrapper.in("project_id", projectIdList);
        }

        if (!CollectionUtils.isEmpty(permissionIdList)) {
            updateWrapper.in("permission_id", permissionIdList);
        }
        projectPermissionDao.delete(updateWrapper);
    }

    /**
     * 根据权限id找出项目id
     *
     * @param permissionIdList
     * @return
     */
    @Override
    public List<Integer> getProjectIdByPermissionId(List<Integer> permissionIdList) {
        QueryWrapper<ProjectPermission> projectPermissionQueryWrapper = new QueryWrapper<>();
        projectPermissionQueryWrapper.select("project_id");
        projectPermissionQueryWrapper.in("permission_id", permissionIdList);
        List<ProjectPermission> projectPermissionList = projectPermissionDao.selectList(projectPermissionQueryWrapper);
        List<Integer> projectIdList = projectPermissionList.stream().map(e -> e.getProjectId()).collect(Collectors.toList());
        return projectIdList;
    }

    /**
     * 根据项目id分组 获取项目权限数据
     *
     * @param projectIdList
     * @return
     */
    @Override
    public List<ProjectPermission> getListGroupByProject(List<Integer> projectIdList) {

        QueryWrapper<ProjectPermission> projectPermissionQueryWrapper = new QueryWrapper<>();
        String[] selectColumn = new String[2];
        selectColumn[0] = "project_id";
        selectColumn[1] = "permission_id";
        projectPermissionQueryWrapper.select(selectColumn);
        projectPermissionQueryWrapper.in("project_id", projectIdList);
        String[] groupByColumn = new String[2];
        groupByColumn[0] = "project_id";
        groupByColumn[1] = "permission_id";
        projectPermissionQueryWrapper.groupBy(groupByColumn);
        List<ProjectPermission> projectPermissions = projectPermissionDao.selectList(projectPermissionQueryWrapper);
        return projectPermissions;
    }

    /**
     * 根据项目id批量删除
     *
     * @param projectId
     * @param permissionIdList
     */
    @Override
    public void deleteBatchByProjectId(Integer projectId, List<Integer> permissionIdList) {
        QueryWrapper<ProjectPermission> deleteMap = new QueryWrapper<>();
        deleteMap.eq("project_id", projectId);
        deleteMap.in("permission_id", permissionIdList);
        projectPermissionDao.delete(deleteMap);

    }

    @Override
    public List<ProjectPermission> getList(List<Integer> projectIdList, List<Integer> permissionIdList) {

        QueryWrapper<ProjectPermission> projectPermissionQueryWrapper = new QueryWrapper<>();
        if (!CollectionUtils.isEmpty(projectIdList)) {
            projectPermissionQueryWrapper.in("permission_id", permissionIdList);
        }
        if (!CollectionUtils.isEmpty(permissionIdList)) {
            projectPermissionQueryWrapper.in("project_id", projectIdList);
        }
        List<ProjectPermission> projectPermissionList = projectPermissionDao.selectList(projectPermissionQueryWrapper);
        return projectPermissionList;
    }


}
